package com.zhht.checkcop.parser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.mysql.jdbc.StringUtils;
import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class ParserMovieInfo implements Pipeline{
	private final static Logger log= Logger.getLogger(ParserMovieInfo.class);
	private String type;
	public ParserMovieInfo(){}
	public ParserMovieInfo(String type){
		this.type=type;
	}
	public void process(ResultItems resultItems, Task arg1) {
		Map<String, Object> mapResults = resultItems.getAll();
        Iterator<Entry<String, Object>> iter = mapResults.entrySet().iterator();
        Map.Entry<String, Object> entry;
        
        String description="";
        String magnet="";
        String bt="";
        String ftp="";
        String infoUrl="";
        String updateTime="";
        List<String> infoPicUrl=null;
        // 输出到控制台
        while (iter.hasNext()) {
            entry = iter.next();
            if(entry.getKey().equals("description")){
            	description=entry.getValue() + "";
            }
            if(entry.getKey().equals("updateTime")){
            	updateTime = entry.getValue() + "";
            }
            if(entry.getKey().equals("magnet")){
            	magnet=entry.getValue() + "";
            }
            if(entry.getKey().equals("bt")){
            	bt = entry.getValue() + "";
            }
            if(entry.getKey().equals("ftp")){
            	ftp = entry.getValue() + "";
            }
            if(entry.getKey().equals("infoUrl")){
            	infoUrl = entry.getValue() + "";
            }
            if(entry.getKey().equals("infoPicUrl")){
            	if(entry.getValue()!=null){
            		infoPicUrl = (List<String>) entry.getValue();
            	}
            }
        }
        
        try{
        	
        	Map<String, Object> exitNovelMap = StaticDBMap.existNovelNameMap.get(infoUrl);
        	
        	if(StringUtils.isNullOrEmpty(description)){
        		return;
        	}
        	//新增
        	exitNovelMap.put("description", description);
        	exitNovelMap.put("infoMagnetUrl", magnet);
        	exitNovelMap.put("infoBtUrl", bt);
        	exitNovelMap.put("infoFtpUrl", ftp);
        	exitNovelMap.put("updateTime", updateTime);
	        
        	execInsertDB(exitNovelMap);
        	
        	if(infoPicUrl==null)
        		return;
        	//新增
        	execUpdateDB(infoPicUrl,exitNovelMap.get("id")+"");
        	
        }catch(Exception e){
    		e.printStackTrace();
    	}
	}
	
	public void execInsertDB(Map<String ,Object> updateNovel){
		MysqlDb mysqldb = null;
		try{
			mysqldb = new MysqlDb();
			mysqldb.connect();
			
			//更新小说
			Map<String ,Object> condition=new HashMap<String, Object>();
			condition.put("id", updateNovel.remove("id"));
			mysqldb.update("movie_info", updateNovel, null, condition);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				mysqldb.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void execUpdateDB(Map<String ,Object> updateNovel,List<String> catalog,List<String> catalogName,int nowChapterCount, String novelUrl){
		MysqlDb mysqldb = null;
		try{
			mysqldb = new MysqlDb();
			mysqldb.connect();
			mysqldb.startTrans();
			
			//更新小说
			Map<String ,Object> condition=new HashMap<String, Object>();
			condition.put("id", updateNovel.remove("id"));
			mysqldb.update("novel_info", updateNovel, null, condition);
			
			List<String[]> sqls=new ArrayList<String[]>();
			//批量插入章节
			for(int i=nowChapterCount;i<catalog.size();i++){
				Map<String,Object> map=new HashMap<String, Object>();
				map.put("novel_id", condition.get("id"));
				map.put("cname", catalogName.get(i));
				map.put("sequence", i);
				map.put("url", novelUrl+catalog.get(i));
				sqls.add(mysqldb.assemblyInsertSqls(StaticDBMap.classfiyTableName.get(type).get("tableName")+"",map));
			}
			if(sqls.size()>0){
				mysqldb.executeBatchs(sqls);
			}
			mysqldb.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				mysqldb.rollback();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			try {
				mysqldb.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	public void execUpdateDB(List<String> catalogName,String id){
		MysqlDb mysqldb = null;
		try{
			mysqldb = new MysqlDb();
			mysqldb.connect();
			
			List<String[]> sqls=new ArrayList<String[]>();
			//批量插入章节
			for(int i=0;i<catalogName.size();i++){
				Map<String,Object> map=new HashMap<String, Object>();
				map.put("movieID", id);
				map.put("picURL", catalogName.get(i));
				sqls.add(mysqldb.assemblyInsertSqls("picUrl",map));
			}
			if(sqls.size()>0){
				mysqldb.executeBatchs(sqls);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				mysqldb.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
